package com.cdm.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cdm.pojo.Activity;
import com.cdm.service.CollectService;

@Controller
@RequestMapping("/collect")
public class CollectController {

	@Autowired
	private CollectService collectService;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping("/addCollect")
	public void addCollect(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		System.out.println("=="+activityId+userId);
		map.put("activityId", activityId);
		map.put("userId", userId);
		Boolean sucess = collectService.addCollect(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/deleteCollect")
	public void deleteJoiner(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		System.out.println("=="+activityId+userId);
		map.put("activityId", activityId);
		map.put("userId", userId);
		Boolean sucess = collectService.deleteCollect(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/findCollectById")
	public void findCollect(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userId = request.getParameter("userId");
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		map.put("userId", userId);
		List<Map<String, Object>> collections = collectService.findCollect(map);
		Integer infoSum = collectService.findCollectCount(map);
		JSONObject jsonObject=new JSONObject();
		if (collections!=null && collections.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("collectionList", collections);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
}
